<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组中有一个数字出现的次数超过数组长度的一半，请找出这个数</title>
</head>

<body>
  <script>
    function arr(nums) {
      var obj = {};
      for (let i = 0; i < nums.length; i++) {
        if (obj[nums[i]] === undefined) {
          obj[nums[i]] = 1;
        } else {
          obj[nums[i]]++;
        }
      }
      var temp = nums.length / 2;
      for (let i in obj) {//是为遍历对象属性而构建的
        if (temp <= obj[i]) {
          return i;
        }
      }
    };
    console.log(arr([1, 2, 3, 2, 2, 2, 5, 4, 2]));

  </script>
</body>

</html>